{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Monotonic Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isMonotonic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #单调数列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>如果数组是单调递增或单调递减的，那么它是&nbsp;<strong>单调 </strong><em>的</em>。</p>\n",
    "\n",
    "<p>如果对于所有 <code>i &lt;= j</code>，<code>nums[i] &lt;= nums[j]</code>，那么数组 <code>nums</code> 是单调递增的。 如果对于所有 <code>i &lt;= j</code>，<code>nums[i]&gt; = nums[j]</code>，那么数组 <code>nums</code>&nbsp;是单调递减的。</p>\n",
    "\n",
    "<p>当给定的数组 <code>nums</code>&nbsp;是单调数组时返回 <code>true</code>，否则返回 <code>false</code>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<ol>\n",
    "</ol>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,2,2,3]\n",
    "<strong>输出：</strong>true\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [6,5,4,4]\n",
    "<strong>输出：</strong>true\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,3,2]\n",
    "<strong>输出：</strong>false\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>-10<sup>5</sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>5</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [monotonic-array](https://leetcode.cn/problems/monotonic-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [monotonic-array](https://leetcode.cn/problems/monotonic-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,2,3]', '[6,5,4,4]', '[1,3,2]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        \n",
    "        direction = 0\n",
    "        \n",
    "        for i, v in enumerate(A):\n",
    "            if i > 0:\n",
    "                if v > A[i-1]:\n",
    "                    if direction < 0 :\n",
    "                        return False\n",
    "                    else:\n",
    "                        direction = 1\n",
    "                elif v < A[i-1]:\n",
    "                    if direction > 0:\n",
    "                        return False\n",
    "                    else:\n",
    "                        direction = -1\n",
    "        else:\n",
    "            return True\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if len(A)<3 or len(set(A))==1:\n",
    "            return True\n",
    "        \n",
    "        scale = A[0]-A[-1]\n",
    "        for i in range(len(A)-1):\n",
    "            if (A[i]-A[i+1])*scale<0 or scale == 0:\n",
    "                return False\n",
    "        return True\n",
    "    \n",
    "    # def isMonotonic(self, A):\n",
    "    #     \"\"\"\n",
    "    #     :type A: List[int]\n",
    "    #     :rtype: bool\n",
    "    #     \"\"\"\n",
    "    #     if A == sorted(A) or A == sorted(A, reverse = True):\n",
    "    #         return True\n",
    "    #     else:\n",
    "    #         return False\n",
    "        \n",
    "        \n",
    "    # def isMonotonic(self, A):\n",
    "    #     \"\"\"\n",
    "    #     :type A: List[int]\n",
    "    #     :rtype: bool\n",
    "    #     \"\"\"\n",
    "    #     if not A or len(A)<3:\n",
    "    #         return True\n",
    "    #     pre = A[0]\n",
    "    #     if A[0] <= A[-1]:\n",
    "    #         sign = \"up\"\n",
    "    #         for i in range(1, len(A)):\n",
    "    #             now = A[i]\n",
    "    #             if now < pre:\n",
    "    #                 return False\n",
    "    #             else:\n",
    "    #                 pre = now\n",
    "    #     else:\n",
    "    #         sign = \"down\"\n",
    "    #         for i in range(1, len(A)):\n",
    "    #             now = A[i]\n",
    "    #             if now > pre:\n",
    "    #                 return False\n",
    "    #             else:\n",
    "    #                 pre = now\n",
    "    #     return True\n",
    "    \n",
    "    # def isMonotonic(self,A):\n",
    "    #     \"\"\"\n",
    "    #     :type A: List[int]\n",
    "    #     :rtype: bool\n",
    "    #     \"\"\"\n",
    "    #     if not A or len(A)<3 or len(set(A))==1:\n",
    "    #         return True\n",
    "    #     pre = A[0]\n",
    "    #     scale = A[0]-A[-1]\n",
    "    #     for i in range(1, len(A)):\n",
    "    #         now = A[i]\n",
    "    #         if (pre-now)*scale < 0 or scale == 0 :\n",
    "    #             return False\n",
    "    #         else:\n",
    "    #             pre = now\n",
    "    #     return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        inc, dec = False, False\n",
    "        for i in range(1, len(A)):\n",
    "            a, b = A[i-1], A[i]\n",
    "            if a < b:\n",
    "                inc = True\n",
    "            if a >b:\n",
    "                dec = True\n",
    "        return not inc or not dec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        if len(A) <= 1: return True\n",
    "        n = len(A) - 1\n",
    "        for i in range(n):\n",
    "            A[i] = A[i] - A[i+1]\n",
    "        A.pop()\n",
    "        A.sort()\n",
    "        return A[0] * A[-1] >= 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        i = 0\n",
    "        while i+1 < len(A) and A[i]==A[i+1]:\n",
    "            i = i+1\n",
    "        if i+1 == len(A):\n",
    "            return True\n",
    "        if A[i] < A[i+1]:\n",
    "            for x in range(i, len(A)-1):\n",
    "                if(A[x+1] < A[x]):\n",
    "                    return False\n",
    "        else:\n",
    "            for x in range(i, len(A)-1):\n",
    "                if(A[x+1] > A[x]):\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: 'List[int]') -> 'bool':\n",
    "        return A == sorted(A) or A == sorted(A, reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def isMonotonic(self, A):\n",
    "        increasing = decreasing = True\n",
    "\n",
    "        for i in range(len(A) - 1):\n",
    "            if A[i] > A[i+1]:\n",
    "                increasing = False\n",
    "            if A[i] < A[i+1]:\n",
    "                decreasing = False\n",
    "\n",
    "        return increasing or decreasing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        #法1\n",
    "        # B=A.copy()\n",
    "        # C=A.copy()\n",
    "        # B.sort()\n",
    "        # C.sort(reverse=True)\n",
    "        # if A==B or A==C:\n",
    "        #     return True\n",
    "        # else:\n",
    "        #     return False\n",
    "        \n",
    "        #法2\n",
    "        #return A == sorted(A) or A == sorted(A)[::-1]\n",
    "    \n",
    "        #法3\n",
    "        # if A==sorted(A)or A==sorted(A,reverse=True):\n",
    "        #     return True\n",
    "        # else:\n",
    "        #     return False\n",
    "        \n",
    "        return A == sorted(A) or A == sorted(A,reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        s= sorted(A)\n",
    "        s1 =sorted(A,reverse =  True)\n",
    "        if A == s or A == s1:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        flag = None\n",
    "        tmp = A.pop(0)\n",
    "        while(A):\n",
    "            tmp2 = A.pop(0)\n",
    "            if tmp > tmp2 and flag != False:\n",
    "                tmp = tmp2\n",
    "                flag = True \n",
    "                continue\n",
    "            elif tmp < tmp2 and flag != True:\n",
    "                flag = False\n",
    "                tmp = tmp2\n",
    "                continue\n",
    "            elif tmp == tmp2:\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        if(len(A)<3):\n",
    "            return True\n",
    "        for i in range(1,len(A)):\n",
    "            A[i-1]-=A[i]\n",
    "        A.pop(len(A)-1)\n",
    "        isup=0;\n",
    "        isdown=0;\n",
    "        for i in A:\n",
    "            if i > 0:\n",
    "                isup+=1\n",
    "            if i<0:\n",
    "                isdown+=1\n",
    "        if isup==0 or isdown==0:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return A == sorted(A) or A == sorted(A, reverse=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        upp, low = 0, 0\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] > nums[i-1]:\n",
    "                upp += 1\n",
    "            elif nums[i] < nums[i-1]:\n",
    "                low += 1\n",
    "        if upp > 0 and low > 0:\n",
    "            return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        assert isinstance(nums, list)\n",
    "            \n",
    "        temp = sorted(nums)\n",
    "        if temp == nums or list(reversed(temp)) == nums:\n",
    "            return True\n",
    "\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        assert isinstance(nums, list)\n",
    "            \n",
    "        temp = sorted(nums)\n",
    "        if temp == nums or list(reversed(temp)) == nums:\n",
    "            return True\n",
    "\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        if isinstance(nums, list) is False:\n",
    "            return False\n",
    "\n",
    "        temp = sorted(nums)\n",
    "        if temp == nums or list(reversed(temp)) == nums:\n",
    "            return True\n",
    "\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        new=[i for i in nums]\n",
    "        nums=sorted(nums)\n",
    "        if new==nums or new ==nums[::-1]:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        t=sorted(A)\n",
    "        return A in (t,t[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        if len(A) <= 2:\n",
    "            return True\n",
    "        \n",
    "        if A[0] == A[-1]:\n",
    "            return len(set(A)) == 1\n",
    "        elif A[0] < A[-1]:\n",
    "            flag = 1\n",
    "        else:\n",
    "            flag = -1\n",
    "        \n",
    "        for i in range(1, len(A)):\n",
    "            if A[i - 1] * flag > A[i] * flag:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        n = len(A)\n",
    "        num = A\n",
    "        if n < 3:\n",
    "            return True\n",
    "\n",
    "        flag = 0\n",
    "        for idx in range(1, n):\n",
    "            if num[idx] > num[idx-1]:\n",
    "                if flag < 0:\n",
    "                    return False\n",
    "                flag = 1\n",
    "            elif num[idx] < num[idx-1]:\n",
    "                if flag > 0:\n",
    "                    return False\n",
    "                flag = -1\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        B = []\n",
    "        for i in A:\n",
    "            B.append(i)\n",
    "\n",
    "        list.sort(A,reverse=True)\n",
    "        C = []\n",
    "        for j in A :\n",
    "            C.append(j)\n",
    "        list.sort(A)\n",
    "        D = []\n",
    "        for k in A :\n",
    "            D.append(k)\n",
    "        return C == B or D == B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        result = True\n",
    "        if len(A) != 1:\n",
    "            b1 = A[1:]\n",
    "            b2 = A[0:-1]\n",
    "            b = [b2[i] - b1[i] for i in range(len(b1))]\n",
    "            if max(b) * min(b) < 0:\n",
    "                result = False\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        n = len(A)\n",
    "        if n == 1:\n",
    "            return True\n",
    "        \n",
    "        dp = [[False] * 2 for _ in range(n)]\n",
    "        dp[0][0] = True\n",
    "        dp[0][1] = True\n",
    "\n",
    "        for i in range(1, n):\n",
    "            if dp[i-1][0]:\n",
    "                dp[i][0] = A[i] >= A[i-1]\n",
    "            if dp[i-1][1]:\n",
    "                dp[i][1] = A[i] <= A[i-1]\n",
    "\n",
    "        return dp[n-1][0] or dp[n-1][1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\r\n",
    "\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\r\n",
    "        for i in range(1, len(nums)):\r\n",
    "            if nums[i] < nums[i - 1]:\r\n",
    "                nums[i - 1] = -1\r\n",
    "            elif nums[i] == nums[i - 1]:\r\n",
    "                nums[i - 1] = 0\r\n",
    "            elif nums[i] > nums[i - 1]:\r\n",
    "                nums[i - 1] = 1\r\n",
    "        nums[-1] = 0\r\n",
    "        return nums.count(1) == 0 or nums.count(-1) == 0\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, A: List[int]) -> bool:\n",
    "        ltincrease = sorted(A)\n",
    "        ltdecrease = sorted(A, reverse = True)\n",
    "        sincrease = ''.join(str(ltincrease))\n",
    "        sdecrease = ''.join(str(ltdecrease))\n",
    "        s = ''.join(str(A))\n",
    "        if s == sincrease:\n",
    "            return True\n",
    "        if s ==sdecrease:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        res1 = sorted(nums)\n",
    "        res2 = sorted(nums,reverse = True)\n",
    "        if nums == res1 or nums == res2:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        if len(nums) < 3:\n",
    "            return True\n",
    "        pre = nums[1]\n",
    "        order  = nums[1] - nums[0]\n",
    "        for i in range(2, len(nums)):\n",
    "            if order  == 0:\n",
    "                order = nums[i] - pre\n",
    "                if order  == 0:\n",
    "                    continue\n",
    "            if order >0 and nums[i] >= pre:\n",
    "                pre = nums[i]\n",
    "                continue\n",
    "            if order < 0 and nums[i] <= pre:\n",
    "                pre = nums[i]\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums) -> bool:\n",
    "        length = len(nums)\n",
    "        if length == 1:\n",
    "            return True\n",
    "        flag = 0\n",
    "        for i in range(0, length - 1):\n",
    "            if (flag == 1 and nums[i] > nums[i+1]) or (flag == 2 and nums[i] < nums[i+1]):\n",
    "                return False\n",
    "            if nums[i] < nums[i+1]:\n",
    "                flag = 1\n",
    "            elif nums[i] > nums[i+1]:\n",
    "                flag = 2\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        # 判断是否单调递增\n",
    "        upFlag = True\n",
    "        for i in range(len(nums)-1):\n",
    "            if not (nums[i] <= nums[i+1]):\n",
    "                upFlag = False\n",
    "                break\n",
    "            i += 1\n",
    "        \n",
    "        # 判断是否单调递减\n",
    "        downFlag = True\n",
    "        for i in range(len(nums)-1):\n",
    "            if not (nums[i] >= nums[i+1]):\n",
    "                downFlag = False\n",
    "                break\n",
    "            i += 1\n",
    "        \n",
    "        return upFlag or downFlag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "       return (sorted(nums)==nums or sorted(nums,reverse = True) == nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        increase = False\n",
    "        decrease = False\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            if nums[i]<nums[i+1]:\n",
    "                increase = True\n",
    "            elif nums[i]>nums[i+1]:\n",
    "                decrease = True\n",
    "        return not(increase and decrease)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        flag = 0\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i] > nums[i+1]:\n",
    "                flag = 1\n",
    "                break\n",
    "        if flag == 1:\n",
    "            for i in range(len(nums)-1):\n",
    "                if nums[i] < nums[i+1]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        a=True\n",
    "        b=True\n",
    "        for i in range (len(nums)-1):\n",
    "            if nums[i]<nums[i+1]:\n",
    "                a=False                           \n",
    "            if nums[i]>nums[i+1]:\n",
    "                b=False\n",
    "            if not a and not b:\n",
    "                return False\n",
    "        return True\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def isMonotonic(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        nlen = len(nums)\n",
    "        if nlen <= 2:\n",
    "            return True\n",
    "        else:\n",
    "            inc, dec = True, True\n",
    "            for i in range(1, nlen):\n",
    "                if inc and nums[i-1] > nums[i]:\n",
    "                    inc = False\n",
    "                if dec and nums[i-1] < nums[i]:\n",
    "                    dec = False\n",
    "                if not inc and not dec:\n",
    "                    return False\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        if nums==sorted(nums) or nums==sorted(nums)[::-1]:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        arr = sorted(nums)\n",
    "        return nums == arr or nums == arr[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isMonotonic(self, nums: List[int]) -> bool:\n",
    "        n=len(nums)\n",
    "        if n==1:\n",
    "            return bool(1)\n",
    "        if nums[0]>nums[-1]:\n",
    "            a=sorted(nums,reverse=True)\n",
    "        else:\n",
    "            a=sorted(nums)\n",
    "        if a==nums:\n",
    "            return bool(1)\n",
    "        else:\n",
    "            return bool(0)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
